package com.sise.cn.wechat.config;

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @ClassName WechatApplication
 * @Description
 * @Author CCJ
 * @Date 2020/6/2 21:24
 **/
@Component
@EnableConfigurationProperties(value = {WechatConfig.class})
public class WechatApplication {

    @Autowired
    private WechatConfig wechatConfig;

    @Bean
    public WxMpService wxMpService(){
        WxMpService wxMpService=new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage(){
        WxMpInMemoryConfigStorage wxMpConfigStorage=new WxMpInMemoryConfigStorage();
        wxMpConfigStorage.setAppId(wechatConfig.getAppId());
        wxMpConfigStorage.setSecret(wechatConfig.getAppSecret());
        return wxMpConfigStorage;
    }

    @Bean
    public WxMpMessageRouter wxMpMessageRouter(WxMpService wxMpService){
        return new WxMpMessageRouter(wxMpService);
    }
}
